package com.coustemview.qianfeng.huaqiangbeistore.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.activity.Mainpage_oversear_shopActivity;
import com.coustemview.qianfeng.huaqiangbeistore.application.Myapplication;
import com.coustemview.qianfeng.huaqiangbeistore.model.Mainpage_Oversear_handler;
import com.coustemview.qianfeng.huaqiangbeistore.model.Shopcar_list_handler;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mingma on 2016/8/29.
 */
public class voersear_listview_adapter extends BaseAdapter {
    private List<Mainpage_Oversear_handler>data;
    private Context context;
    private List<Shopcar_list_handler>listdata;
   private static final int OVERSEAR_VIEW_ONE=0;
   private static final int OVERSEAR_VIEW_TWO=1;
   private static final int OVERSEAR_VIEW_THREE=2;
    public voersear_listview_adapter(List<Mainpage_Oversear_handler> data, Context context) {
        this.data = data;
        this.context = context;
        listdata=new ArrayList<>();
    }

    public  List<Shopcar_list_handler> getdata(){
       return listdata;
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        int viewType = getItemViewType(position);
        Mainpage_Oversear_handler handler = data.get(position);
        switch (viewType) {
           case OVERSEAR_VIEW_ONE :
             ViewHolder1 holder1=null;
               if(convertView==null) {
                  convertView = LayoutInflater.from(context).inflate(R.layout.fragment_oversear_item, null);
                   holder1=new ViewHolder1(convertView);
                   convertView.setTag(holder1);
               }else{
                   holder1= (ViewHolder1) convertView.getTag();
               }
               if(TextUtils.isEmpty(handler.headtitle)) {
                  holder1.text_oversear_headtitle.setVisibility(View.GONE);
               }else{
                 holder1.text_oversear_headtitle.setText(handler.headtitle);
                holder1.text_oversear_headtitle.setVisibility(View.VISIBLE);
               }
               holder1.text_oversear_oldprice.setText(handler.oldPrice);
               holder1.text_oversear_price.setText(handler.price);
               holder1.text_oversear_explain.setText(handler.title);
               holder1.text_oversear_address.setText(handler.nation);
               Picasso.with(context).load(handler.skuImage).into(holder1.image_oversear_picture);
               if(handler.nation.equals("hk")) {
                  holder1.image_oversear_nation.setImageResource(R.mipmap.hongkong);
               }else if(handler.nation.equals("europe")) {
                 holder1.image_oversear_nation.setImageResource(R.mipmap.europeanunion);    
               }else if(handler.nation.equals("usa")) {
                  holder1.image_oversear_nation.setImageResource(R.mipmap.america);
               }
               holder1.button_oversear_shopcar.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Shopcar_list_handler listHandler=new Shopcar_list_handler();
                       Mainpage_Oversear_handler handler1 = data.get(position);
                        listHandler.oldprice= handler1.oldPrice;
                        listHandler.price = handler1.price;
                       listHandler.photo = handler1.skuImage;
                       listHandler.shoptitle = handler1.title;
                       listHandler.ischecked=true;
                       listHandler.count=1;
                       listdata.add(listHandler);
                   }
               });
               holder1.mRelativeLayout1.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Intent intent=new Intent(context, Mainpage_oversear_shopActivity.class);
                       Mainpage_Oversear_handler handler = data.get(position);
                         String skuId = handler.skuId;
                       String price = handler.price;
                       String oldPrice = handler.oldPrice;
                       intent.putExtra("price",price);
                       intent.putExtra("oldprice",oldPrice);
                       intent.putExtra("skuid",skuId);
                       context.startActivity(intent);
                   }
               });
               break;
           case OVERSEAR_VIEW_TWO :
               ViewHolder2 holder2=null;
               if(convertView==null) {
                   convertView = LayoutInflater.from(context).inflate(R.layout.fragment_oversear_item, null);
                   holder2=new ViewHolder2(convertView);
                   convertView.setTag(holder2);
               }else{
                   holder2= (ViewHolder2) convertView.getTag();
               }
               if(TextUtils.isEmpty(handler.headtitle)) {
                   holder2.text_oversear_headtitle.setVisibility(View.GONE);
               }else{
                   holder2.text_oversear_headtitle.setText(handler.headtitle);
                   holder2.text_oversear_headtitle.setVisibility(View.VISIBLE);
               }
               holder2.text_oversear_oldprice.setText(handler.oldPrice);
               holder2.text_oversear_price.setText(handler.price);
               holder2.text_oversear_explain.setText(handler.title);
               holder2.text_oversear_address.setText(handler.nation);
               Picasso.with(context).load(handler.skuImage).into(holder2.image_oversear_picture);
               if(handler.nation.equals("hk")) {
                   holder2.image_oversear_nation.setImageResource(R.mipmap.hongkong);
               }else if(handler.nation.equals("europe")) {
                   holder2.image_oversear_nation.setImageResource(R.mipmap.europeanunion);
               }else if(handler.nation.equals("usa")) {
                   holder2.image_oversear_nation.setImageResource(R.mipmap.america);
               }
               holder2.button_oversear_shopcar.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Shopcar_list_handler listHandler=new Shopcar_list_handler();
                       Mainpage_Oversear_handler handler1 = data.get(position);
                       listHandler.oldprice= handler1.oldPrice;
                       listHandler.price = handler1.price;
                       listHandler.photo = handler1.skuImage;
                       listHandler.shoptitle = handler1.title;
                       listHandler.ischecked=true;
                       listHandler.count=1;
                       listdata.add(listHandler);

                   }
               });
               holder2.mRelativeLayout2.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Intent intent=new Intent(context, Mainpage_oversear_shopActivity.class);
                       Mainpage_Oversear_handler handler = data.get(position);
                       String skuId = handler.skuId;
                       String price = handler.price;
                       String oldPrice = handler.oldPrice;
                       intent.putExtra("price",price);
                       intent.putExtra("oldprice",oldPrice);
                       intent.putExtra("skuid",skuId);
                       context.startActivity(intent);
                   }
               });
               break;
           case OVERSEAR_VIEW_THREE:
               ViewHolder3 holder3=null;
               if(convertView==null) {
                   convertView = LayoutInflater.from(context).inflate(R.layout.fragment_oversear_item, null);
                   holder3=new ViewHolder3(convertView);
                   convertView.setTag(holder3);
               }else{
                   holder3= (ViewHolder3) convertView.getTag();
               }
               if(TextUtils.isEmpty(handler.headtitle)) {
                   holder3.text_oversear_headtitle.setVisibility(View.GONE);
               }else{
                   holder3.text_oversear_headtitle.setText(handler.headtitle);
                   holder3.text_oversear_headtitle.setVisibility(View.VISIBLE);
               }
               holder3.text_oversear_oldprice.setText(handler.oldPrice);
               holder3.text_oversear_price.setText(handler.price);
               holder3.text_oversear_explain.setText(handler.title);
               holder3.text_oversear_address.setText(handler.nation);
               Picasso.with(context).load(handler.skuImage).into(holder3.image_oversear_picture);
               if(handler.nation.equals("hk")) {
                   holder3.image_oversear_nation.setImageResource(R.mipmap.hongkong);
               }else if(handler.nation.equals("europe")) {
                   holder3.image_oversear_nation.setImageResource(R.mipmap.europeanunion);
               }else if(handler.nation.equals("usa")) {
                   holder3.image_oversear_nation.setImageResource(R.mipmap.america);
               }
               holder3.button_oversear_shopcar.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Shopcar_list_handler listHandler=new Shopcar_list_handler();
                       Mainpage_Oversear_handler handler1 = data.get(position);
                       listHandler.oldprice= handler1.oldPrice;
                       listHandler.price = handler1.price;
                       listHandler.photo = handler1.skuImage;
                       listHandler.shoptitle = handler1.title;
                       listHandler.ischecked=true;
                       listHandler.count=1;
                       listdata.add(listHandler);

                   }
               });
               holder3.mRelativeLayout3.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Intent intent=new Intent(context, Mainpage_oversear_shopActivity.class);
                       Mainpage_Oversear_handler handler = data.get(position);
                       String skuId = handler.skuId;
                       String price = handler.price;
                       String oldPrice = handler.oldPrice;
                       intent.putExtra("price",price);
                       intent.putExtra("oldprice",oldPrice);
                       intent.putExtra("skuid",skuId);
                       context.startActivity(intent);
                   }
               });
               break;
           default:
               break;
       }
        return convertView;
    }
   static class ViewHolder1{
    private TextView text_oversear_headtitle,text_oversear_address,text_oversear_explain,text_oversear_price,text_oversear_oldprice;
    private ImageView image_oversear_picture,image_oversear_nation;
   private Button button_oversear_shopcar;
   private RelativeLayout mRelativeLayout1;
       public ViewHolder1(View view) {
          text_oversear_address= (TextView) view.findViewById(R.id.text_oversear_address);
          text_oversear_explain= (TextView) view.findViewById(R.id.text_oversear_explain);
           text_oversear_price= (TextView) view.findViewById(R.id.text_oversear_price);
           text_oversear_oldprice= (TextView) view.findViewById(R.id.text_oversear_oldprice);
          text_oversear_headtitle= (TextView) view.findViewById(R.id.headtitle_oversear);
          image_oversear_picture= (ImageView) view.findViewById(R.id.image_oversear_picture);
           image_oversear_nation= (ImageView) view.findViewById(R.id.image_oversear_nation);
           button_oversear_shopcar= (Button) view.findViewById(R.id.button_oversear_shopcar);
           mRelativeLayout1= (RelativeLayout) view.findViewById(R.id.relativelayout_oversear_item);
       }
   }
    static class ViewHolder2{
        private TextView text_oversear_headtitle,text_oversear_address,text_oversear_explain,text_oversear_price,text_oversear_oldprice;
        private ImageView image_oversear_picture,image_oversear_nation;
        private Button button_oversear_shopcar;
        private RelativeLayout mRelativeLayout2;
        public ViewHolder2(View view) {
            text_oversear_address= (TextView) view.findViewById(R.id.text_oversear_address);
            text_oversear_explain= (TextView) view.findViewById(R.id.text_oversear_explain);
            text_oversear_price= (TextView) view.findViewById(R.id.text_oversear_price);
            text_oversear_oldprice= (TextView) view.findViewById(R.id.text_oversear_oldprice);
            text_oversear_headtitle= (TextView) view.findViewById(R.id.headtitle_oversear);
            image_oversear_picture= (ImageView) view.findViewById(R.id.image_oversear_picture);
            image_oversear_nation= (ImageView) view.findViewById(R.id.image_oversear_nation);
            button_oversear_shopcar= (Button) view.findViewById(R.id.button_oversear_shopcar);
            mRelativeLayout2= (RelativeLayout) view.findViewById(R.id.relativelayout_oversear_item);
        }

    }
    static class ViewHolder3{
        private TextView text_oversear_headtitle,text_oversear_address,text_oversear_explain,text_oversear_price,text_oversear_oldprice;
        private ImageView image_oversear_picture,image_oversear_nation;
        private Button button_oversear_shopcar;
         private RelativeLayout mRelativeLayout3;
        public ViewHolder3(View view) {
            text_oversear_address= (TextView) view.findViewById(R.id.text_oversear_address);
            text_oversear_explain= (TextView) view.findViewById(R.id.text_oversear_explain);
            text_oversear_price= (TextView) view.findViewById(R.id.text_oversear_price);
            text_oversear_oldprice= (TextView) view.findViewById(R.id.text_oversear_oldprice);
            text_oversear_headtitle= (TextView) view.findViewById(R.id.headtitle_oversear);
            image_oversear_picture= (ImageView) view.findViewById(R.id.image_oversear_picture);
            image_oversear_nation= (ImageView) view.findViewById(R.id.image_oversear_nation);
            button_oversear_shopcar= (Button) view.findViewById(R.id.button_oversear_shopcar);
            mRelativeLayout3= (RelativeLayout) view.findViewById(R.id.relativelayout_oversear_item);
        }

    }
    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).category;
    }
}
